var app = getApp()

Page({
  data: {
    selectId: ''
  },
  onLoad: function (options) {
    var selectid = options.selected
    if (selectid == null || selectid == 'null' || selectid == undefined || selectid == 'undefined') {
      selectid = ''
    }
    this.setData({
      selectId: selectid
    })
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        var iconhgt = (res.windowWidth - 20) * 252 / 681
        that.setData({
          iconhgt: iconhgt
        })
      }
    })

    var validCoupons = [], invalidCoupons = []
    var couponlist = JSON.parse(options.coupon)
    for (var i = 0; i < couponlist.AVAILABLEPROMOTIONS.length; i++) {
      for (var keystr in couponlist.AVAILABLEPROMOTIONS[i]) {
        var teminfo = couponlist.AVAILABLEPROMOTIONS[i][keystr]
        teminfo.couponid = keystr
        validCoupons.push(teminfo)
      }
    }
    for (var i = 0; i < couponlist.DISABLEPROMOTINS.length; i++) {
      for (var keystr in couponlist.DISABLEPROMOTINS[i]) {
        var teminfo = couponlist.DISABLEPROMOTINS[i][keystr]
        teminfo.couponid = keystr
        invalidCoupons.push(teminfo)
      }
    }

    this.setData({
      validCoupons: validCoupons,
      invalidCoupons: invalidCoupons
    })
  },

  selectCoupon: function (e) {
    var coupon = e.currentTarget.dataset.coupon
    var name = coupon.name + coupon.money + '元'
    app.globalData.tempCoupon = {
      id: coupon.couponid,
      name: name
    }
    wx.navigateBack()
  },
  quitCoupon: function (e) {
    app.globalData.tempCoupon = {
      id: null,
      name: ''
    }
    wx.navigateBack()
  }
})
